﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
using ComunesMGI.DataTypes;
using ComunesMGI.Enum;
using ComunesMGI.Controles;
using OutlookMGI.Logica;
using Microsoft.Office.Interop.Outlook;


namespace OutlookMGI.Forms
{
    partial class IncidentesGUI
    {
        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass("IPM.Note.IncidentesGUI")]
        [Microsoft.Office.Tools.Outlook.FormRegionName("OutlookMGI.IncidentesGUI")]
        public partial class IncidentesGUIFactory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void IncidentesGUIFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
            }
        }

        #endregion

        private List<DataSistema> _sistemasDeCliente;
        private List<DataCliente> _clientes;
        private TablaCamposControl _tablaCampos;

        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void IncidentesGUI_FormRegionShowing(object sender, System.EventArgs e)
        {
            cargarClientes();
        }

        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void IncidentesGUI_FormRegionClosed(object sender, System.EventArgs e)
        {

        }

        private void cargarClientes()
        {
            List<DataCliente> ldc;
            ILogica il = LogicaFactory.getInstance().getILogica();
            ldc = il.getClientes();
            foreach (DataCliente dc in ldc)
            {
                comboBoxClientes.Items.Add(dc.Nombre);
            }
            _clientes = ldc;
        }

        private void cargarSistemas(DataCliente cliente)
        {
            List<DataSistema> lds;
            ILogica il = LogicaFactory.getInstance().getILogica();
            lds = il.getSistemas(cliente);
            foreach (DataSistema ds in lds)
            {
                comboBoxSistemas.Items.Add(ds.Nombre);
            }

            _sistemasDeCliente = lds;
        }

        private void comboBoxClientes_SelectedIndexChanged(object sender, EventArgs e)
        {
            cargarSistemas(_clientes.First(dc => dc.Nombre == comboBoxClientes.SelectedItem.ToString()));
        }

        private void comboBoxSistemas_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataCliente actualDc = _clientes.First(dc => dc.Nombre == comboBoxClientes.SelectedItem.ToString());
            DataSistema actualDs = _sistemasDeCliente.First(ds => ds.Nombre == comboBoxSistemas.SelectedItem.ToString());
            ILogica il = LogicaFactory.getInstance().getILogica();
            List<DataCampo> dataCampos = il.getCampos(actualDc, actualDs);

            // busco si hay valores por defecto 
            foreach (DataCampo dcTemp in dataCampos)
            {
                switch (dcTemp.ParteMail)
                {
                    case ParteMail.FROM:
                        dcTemp.Valor = ((MailItem)this.OutlookItem).SenderName;
                        break;
                    case ParteMail.BODY:
                        dcTemp.Valor = ((MailItem) this.OutlookItem).Body;
                        break;
                    case ParteMail.DATE:
                        dcTemp.Valor = ((MailItem) this.OutlookItem).ReceivedTime.ToString();
                        break;
                    case ParteMail.SUBJECT:
                        dcTemp.Valor = ((MailItem) this.OutlookItem).Subject;
                        break;
                    case ParteMail.NINGUNO:
                        break;
                    default:
                        break;
                }
            }

            _tablaCampos = new TablaCamposControl(dataCampos);
            groupBox1.Controls.Add(_tablaCampos);
        }

        private void buttonImpactar_Click(object sender, EventArgs e)
        {
            try
            {
                ILogica il = LogicaFactory.getInstance().getILogica();
                DataSistema actualDs = _sistemasDeCliente.First(ds => ds.Nombre == comboBoxSistemas.SelectedItem.ToString());
                actualDs.Campos = _tablaCampos.getCampos();
                il.addIncidente(actualDs);
                MessageBox.Show("Incidente agregado correctamente");
                groupBox1.Controls.Clear();
                comboBoxSistemas.Items.Clear();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
